<template>
    <popup
        :show="showEdit"
        title="审核"
        :loading="loading"
        :async-close="true"
        width="460px"
        @close="emits('close')"
        @confirm="handleSubmit"
    >
        <div class="p-6 pb-0">
            <el-form ref="formRef" :model="formData" label-width="80px">
                <el-form-item label="审核结果" prop="verify_status">
                    <el-radio-group v-model="formData.audit_status">
                        <el-radio :value="1" size="large">审核通过</el-radio>
                        <el-radio :value="2" size="large">审核拒绝</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="拒绝原因" prop="verify_result" v-if="formData.audit_status == 2">
                    <el-input
                        v-model="formData.audit_result"
                        type="textarea"
                        :autosize="{ minRows: 8, maxRows: 20 }"
                        placeholder="请输入拒绝原因"
                    />
                </el-form-item>
            </el-form>
        </div>
    </popup>
</template>

<script setup lang="ts">
import feedback from '@/utils/feedback'
import squareVideoApi from '@/api/square/video'

const emits = defineEmits(['success', 'close'])

const formRef = ref()
const showEdit = ref<boolean>(false)

// 表单数据
const loading = ref(false)
const formData: any = reactive({
    id: 0,             // 广场ID
    audit_result: '', // 审核原因
    audit_status: 0   // 审核状态
})

/**
 * 提交表单
 */
const handleSubmit = async (): Promise<void> => {
    await formRef.value?.validate()
    loading.value = true

    await squareVideoApi.audit(formData)
        .finally(() => {
            loading.value = false
        })

    feedback.msgSuccess('操作成功')
    emits('close')
    emits('success')
}

/**
 * 打开表单
 *
 * @param {any} row
 * @returns {Promise<void>}
 */
const open = async (row?: any): Promise<void> => {
    showEdit.value = true
    for (const key in formData) {
        if (row[key] !== null && row[key] !== undefined) {
            formData[key] = row[key]
        }
    }
}

defineExpose({
    open
})
</script>
